import axios from 'axios'
import 'nprogress/nprogress.css'
import NProgress from 'nprogress'
import {getUserTempId} from '@/utils/getUserTemId'
import store from '@/store'


const ajax = axios.create({
    baseURL: 'http://39.98.123.211/',
    timeout: 5000,
})

ajax.interceptors.request.use(config =>{
    NProgress.start()
    config.headers.userTempId =  getUserTempId()
    const token = store.state.user.userInfo.token
    if(token) {
        config.headers.token = token
    }

    return config
})

ajax.interceptors.response.use(response => {
    NProgress.done()
    return response.data
},error => {
    NProgress.done()
    return Promise.reject(new Error(error.message))
})

export default ajax